package com.haruhi.bot.endpoint.ws.server;


import com.haruhi.bot.endpoint.ws.config.EndpointConfiguration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import java.util.List;

@EnableWebSocket
@Component
public class ServerEndpointRegister implements WebSocketConfigurer {

    private final List<EndpointConfiguration.Server> servers;

    public ServerEndpointRegister(EndpointConfiguration configuration) {
        this.servers = configuration.getServers();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        for (EndpointConfiguration.Server server : servers) {
            registry.addHandler(new ServerEndpoint(server.getPath()),server.getPath())
                    .addInterceptors(new WebSocketHandshakeInterceptor(server.getPath(),server.getAccessToken(),server.getMaxConnections()))
                    .setAllowedOrigins("*");
        }

    }
}